package client.taxiapp.com.taxiapp.activity;

import android.app.Activity;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import client.taxiapp.com.taxiapp.R;
import client.taxiapp.com.taxiapp.domain.BaseResponse;
import client.taxiapp.com.taxiapp.domain.User;
import client.taxiapp.com.taxiapp.retrofit.UserService;
import client.taxiapp.com.taxiapp.util.Constant;
import client.taxiapp.com.taxiapp.util.RetrofitUtil;
import client.taxiapp.com.taxiapp.util.Util;
import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

public class MainActivity extends Activity {

    private TextView mLocalText;

    private Button mBut;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();


        initLayout();


        initListener();




    }



    private void initLayout() {
        mLocalText = (TextView) findViewById(R.id.locationText);
        mBut = (Button) findViewById(R.id.locationBut);
    }

    private void initData() {
    }


    private void initListener() {


        mBut.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                mLocalText.setText("");
                Retrofit retrofit = RetrofitUtil.retrofitRequest();

                UserService userService = retrofit.create(UserService.class);
                String userJson = Util.getString(MainActivity.this, Constant.SP_USER_KEY);
                User user = JSON.parseObject(userJson,User.class);
                user.setPassword("1234");


                Call<BaseResponse> call = userService.login(user);

                call.enqueue(new Callback<BaseResponse>() {
                    @Override
                    public void onResponse(Response<BaseResponse> response, Retrofit retrofit) {

                        BaseResponse baseResponse = response.body();

                        if (baseResponse.getMeta().isResult()) {
                            String json = JSON.toJSONString(baseResponse.getData());
                            User user = JSON.parseObject(json,User.class);
                            mLocalText.setText("latitude="+user.getLatitude()+"longitude="+user.getLongitude());


                        } else {
                            String message = baseResponse.getMeta().getMsg();
                            Log.v("Amence","login error "+message);

                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        String message = t.getMessage();

                    }

                });
            }
        });
    }





}
